package com.qf.service.impl;/*
 *徐韬
 *2021/11/5
 *10:54
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.cache.clientCache;
import com.qf.constons.resultCode;
import com.qf.events.updateClientEvents;
import com.qf.exceptions.addDataException;
import com.qf.exceptions.queryDataException;
import com.qf.exceptions.updateException;
import com.qf.mapper.clientMapper;
import com.qf.pojo.MyAuthUser;
import com.qf.pojo.TClientBusiness;
import com.qf.service.clientService;
import com.sun.xml.internal.bind.v2.model.core.ReferencePropertyInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class clientServiceimpl implements clientService {
    private clientCache clientCache;
    private clientMapper clientMapper;
    private ApplicationContext context;
@Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setClientMapper(com.qf.mapper.clientMapper clientMapper) {
        this.clientMapper = clientMapper;
    }

    @Autowired
    public void setClientCache(com.qf.cache.clientCache clientCache) {
        this.clientCache = clientCache;
    }

    @Override
    public PageInfo<TClientBusiness> showAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyAuthUser myAuthUser = (MyAuthUser) principal;
        Long type = myAuthUser.getType();
        if(type!=1){
            throw new queryDataException("您没有权限查看该条数据", resultCode.NO_ROOT);
        }
        List<TClientBusiness> tClientBusinesses = clientCache.getfinlAll();
        PageInfo<TClientBusiness> pageInfo = new PageInfo<>(tClientBusinesses);

        return pageInfo;
    }

    @Override
    public void add(TClientBusiness tClientBusiness) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyAuthUser myAuthUser =(MyAuthUser)principal;
        Long type = myAuthUser.getType();
        if(type !=1){
            throw new addDataException("您没有权限添加该条数据",resultCode.NO_ROOT);
        }
        clientMapper.insert(tClientBusiness);
        context.publishEvent(new updateClientEvents());
    }

    @Override
    public void update(TClientBusiness tClientBusiness) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyAuthUser myAuthUser = (MyAuthUser) principal;
        Long type = myAuthUser.getType();
        if(type!=1){
        throw new updateException("你没有权限修改该条数据",resultCode.NO_ROOT);
        }
        clientMapper.updateClient(tClientBusiness);
        context.publishEvent(new updateClientEvents());
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyAuthUser myAuthUser = (MyAuthUser) principal;
        Long type = myAuthUser.getType();
        if(type != 1){
            throw new updateException("您没有权限删除该条数据",resultCode.NO_ROOT);
        }
        clientMapper.deleteByids(ids);
        context.publishEvent(new updateClientEvents());
    }
}
